package requrest

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-project/dto"
	"go-project/models"
)

type ArticleCategorySearchRequest struct {
	Pagination Pagination
	Name       string `form:"name"`
}

type ArticleCateInsertRequest struct {
	Name   string `json:"name" binding:"required,article_category_name_unique" required_msg:"用户名不能为空" article_category_name_unique_msg:"该分类名称已存在"`
	Desc   string `json:"desc"`
	Weight int8   `json:"weight" binding:"max=100,min=0" max_msg:"权重最大不能大于100" min_msg:"权重最小不能小于0"`
	dto.ValidateBindRequest
}

func (ac_req *ArticleCateInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(ac_req); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", ac_req)
		return err
	}
	return nil
}
func (ac_req *ArticleCateInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	article_cate, ok := data.(*models.ArticleCategory)
	if ok {
		article_cate.Name = ac_req.Name
		article_cate.Weight = ac_req.Weight
		article_cate.Desc = ac_req.Desc
		return nil
	}
	ac_req.Errors = errors.New("数据类型不正确，数据必须是models.ArticleCategory类型")
	return &ac_req.ValidateBindRequest

}

type ArticleCateUpdateRequest struct {
	Id     uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	Name   string `json:"name" binding:"required,article_category_name_unique=update" required_msg:"用户名不能为空" article_category_name_unique_msg:"该分类名称已存在"`
	Desc   string `json:"desc"`
	Weight int8   `json:"weight" binding:"max=100,min=0" max_msg:"权重最大不能大于100" min_msg:"权重最小不能小于0"`
	dto.ValidateBindRequest
}

func (arc_up_req *ArticleCateUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(arc_up_req); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", arc_up_req)
		return err
	}
	return nil
}
func (arc_up_req *ArticleCateUpdateRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	article_cate, ok := data.(*models.ArticleCategory)
	if ok {
		article_cate.ID = arc_up_req.Id
		article_cate.Name = arc_up_req.Name
		article_cate.Weight = arc_up_req.Weight
		article_cate.Desc = arc_up_req.Desc
		return nil
	}
	arc_up_req.Errors = errors.New("数据类型不正确，数据必须是models.ArticleCategory类型")
	return &arc_up_req.ValidateBindRequest

}
